//
//  ZZCTabBarController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCTabBarController.h"
#import "ZZCNavigationController.h"

@interface ZZCTabBarController ()
@property (weak, nonatomic) UIView * tabBarLine;
@property (strong, nonatomic)UIViewController *controller;
@end

@implementation ZZCTabBarController

#pragma mark - Getter

- (UIView *)tabBarLine {
    if (!_tabBarLine) {
        UIView * line = [[UIView alloc] init];
        [self.tabBar addSubview:line];
        _tabBarLine = line;
        line.backgroundColor = [UIColor colorWithHex:0xe8e9e8];
    }
    return _tabBarLine;
}

#pragma mark - Life cycle
+ (void)initialize {
    [super initialize];
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHex:0xEB6927]} forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.tabBarLine.frame = CGRectMake(0, 0, self.tabBar.frame.size.width, 1);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *barItemClassNameArray = @[ZZC_HOME,
                                       ZZC_CLASSIFY,
                                       ZZC_LATEST,
                                       ZZC_SHOPPING,
                                       ZZC_ME];
    NSArray *barItemTitleArray = @[@"首页",
                                   @"分类",
                                   @"最新",
                                   @"  购物车",
                                   @"我的"];
    NSArray *barItemImageNameArray = @[@"main_home_normal",
                                       @"main_sort_normal",
                                       @"mail_latest_normal",
                                       @"mail_shopcart_normal",
                                       @"nain_mine_normal"];
    NSArray *barItemSelectedImageNameArray = @[@"首页icon-点击",
                                               @"分类icon-点击-",
                                               @"最新icon-点击",
                                               @"购物车icon-点击",
                                               @"我的icon-点击"];
    
    for (int i = 0; i < barItemClassNameArray.count; i++) {
        [self addChildViewControllerWithClassName:barItemClassNameArray[i]
                                            title:barItemTitleArray[i]
                                        imageName:barItemImageNameArray[i]
                                selectedImageName:barItemSelectedImageNameArray[i]];
    }

    
    self.automaticallyAdjustsScrollViewInsets = NO;
}


#pragma mark - Private Method
- (void)addChildViewControllerWithClassName:(NSString *)className
                                      title:(NSString *)title
                                  imageName:(NSString *)imageName
                          selectedImageName:(NSString *)selectedImageName {


    ZZCLoginController *vc = [ZZCLoginController new];
    [self.navigationController pushViewController:vc animated:YES];

    _controller = [[NSClassFromString(className) alloc] init];
    [self addChildViewController:_controller];

    UIImage *image = [UIImage imageNamed:imageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    _controller.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    _controller.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    _controller.tabBarItem.title = title;
    
    //包装一个导航控制器
    ZZCNavigationController *nav = [[ZZCNavigationController alloc] initWithRootViewController:_controller];
    
    [self addChildViewController:nav];
    
}

@end
